<?php

namespace app\repositories\eloquent;
use app\repositories\contracts\ConfigInterface;
use app\admin\model\AdminConfig;
use app\facade\Password;
use think\response\Json;
use function foo\func;

/***
 * Class RootProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台配置接口
 */
class ConfigProvider implements ConfigInterface
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取配置分页列表
     */
    public function getConfigListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return AdminConfig::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取配置总数据量
     */
    public function getConfigListCount():int
    {
        return AdminConfig::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取配置列表
     */
    public function getConfigList($orderby='id',$order='asc'):array
    {
        return AdminConfig::order($orderby, $order)->select()->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除配置
     */
    public function delConfigList(array $id):bool
    {
        $list = AdminConfig::select($id);
        $result = $list->delete();
        return $result;
    }

    /***
     * @param array $data
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:43
     * @Description 根据指定参数批量修改配置字段值
     *
     */
    public function editConfigList(array $data)
    {
        //前端应传递如下格式的二维数组
        //var editData = [{'id':id,'key':kei,'value':value},{...}];
        $Root_obj = new AdminConfig;
        foreach ($data as $k=>$v){
            foreach ($v as $key=>$value){
                if(array_key_exists('root_password',$value)){
                    $rootData = Password::EncryPass($value['root_password']);
                    $list[] = ['id'=>$value['id'], 'root_password'=>$rootData['password'],'salt'=>$rootData['salt']];
                }else{
                    $list[] = $value;
                }
            }
        }
        //返回修改后的数据
        return $Root_obj->saveAll($list)->toArray();
    }

    /***
     * @param array $addArray
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增配置
     */
    public function addConfigList(array $addArray)
    {
        $Root_obj = new AdminConfig;
        $list = [];
        foreach ($addArray as $v){
            //先查询用户名是否已存在
            $isExist = $Root_obj->where('root_name',$v['root_name'])->find();
            if($isExist){
                return false;
            }
            //saveAll函数无法自动添加时间戳，手动添加
            $v['create_time'] = date("Y-m-d H:i:s ");
            //加密密码
            $rootData = Password::EncryPass($v['root_password']);
            $v['salt'] = $rootData['salt'];
            $v['root_password'] = $rootData['password'];
            $list[] = $v;
        }
        return $Root_obj->saveAll($list)->toArray();
    }

}